package com.example.travelexpensemanager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.widget.EditText;
import android.widget.Toast;

public class Utils extends Activity {

	public static String string;
	public static SimpleDateFormat dateFormat;
	public static Date todays_date_or_time;

	public static String[] amountTableColumns() {
		String[] columns = { DatabaseOperations.getAmounttabletitle(),
				DatabaseOperations.getAmounttableamount(),
				DatabaseOperations.getAmounttabletotalmembers() };
		return columns;
	}

	public static String[] memberTableColumns() {
		String[] columns = { DatabaseOperations.getMembertabletitle(),
				DatabaseOperations.getMembertablemember(),
				DatabaseOperations.getMembertableamount(),
				DatabaseOperations.getMembertabletoget(),
				DatabaseOperations.getMembertabletogive() };
		return columns;
	}

	public static String[] purposeTableColumns() {
		String[] columns = { DatabaseOperations.getPurposetabletitle(),
				DatabaseOperations.getPurposetablepayingmember(),
				DatabaseOperations.getPurposetablepurpose(),
				DatabaseOperations.getPurposetableamount() };
		return columns;
	}

	/*
	 * Check if the database exists so that if database is deleted in between
	 * then it is detected
	 */
	public static boolean databaseDoesNotExist(Context context) {
		/* Getting the database name */
		String DatabaseName = context.getFilesDir().getParent() + "/databases/"
				+ DatabaseOperations.getDatabaseName();
		/* False means database does not exist is false i.e database does exist */
		File checkDB = new File(DatabaseName);
		return !checkDB.exists();
		/* return false */
	}

	public static void validateTextLength(EditText editText) {
		if (editText.getText().length() > getTextMaxLength(editText
				.getContext())) {
			string = editText.getText().toString()
					.substring(0, getTextMaxLength(editText.getContext()));
			editText.setText(string);
			Toast.makeText(
					editText.getContext(),
					"Maximum " + getTextMaxLength(editText.getContext())
							+ " characters are allowed", Toast.LENGTH_SHORT)
					.show();
			/* Setting the cursor right at the end */
			editText.setSelection(getTextMaxLength(editText.getContext()));
		}
	}

	public static void validateNumberTextLength(EditText editText) {
		if (editText.getText().length() > getNumberMaxLength(editText
				.getContext())) {
			final String str = editText.getText().toString();
			editText.setText(str.substring(0,
					getNumberMaxLength(editText.getContext())));
			Toast.makeText(
					editText.getContext(),
					"Maximum " + getNumberMaxLength(editText.getContext())
							+ " digits are allowed", Toast.LENGTH_SHORT).show();
			editText.setSelection(getNumberMaxLength(editText.getContext()));
		}
	}

	public static int getNumberMaxLength(Context context) {
		return Integer
				.parseInt(context.getString(R.string.maxNumberTextLength));
	}

	public static int getTextMaxLength(Context context) {
		return Integer.parseInt(context.getString(R.string.maxTextLength));
	}
	
	public static void setCursorPosition(EditText editText){
		/*Setting the cursor position*/
		editText.setSelection(editText.getText().length());
	}
	
	public static String getCurrentDate() {
		todays_date_or_time = new Date();
		dateFormat = new SimpleDateFormat("EEE MM/yyyy");
		return dateFormat.format(todays_date_or_time);
	}

	public static String getCurrentTime() {
		todays_date_or_time = new Date();
		dateFormat = new SimpleDateFormat("hh:mm:ss");
		return dateFormat.format(todays_date_or_time);
	}

}
